import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { constantRoutes } from '@/routes/routes'
import useStore from '@/stores'
import { ENV_CONFIG } from '@/config'

const router = createRouter({
  history: createWebHistory((ENV_CONFIG as any)[import.meta.env.MODE].publicPath),
  routes: constantRoutes,
  scrollBehavior: () => ({ left: 0, top: 0 }),
})

// 重置路由
export function resetRouter() {
  const { routes } = useStore()
  routes.permissionRoutes.forEach((route: RouteRecordRaw) => {
    const name = route.name
    if (name && router.hasRoute(name))
      router.removeRoute(name)
  })
}

export default router
